package cuit.game.service;

import cuit.game.dao.TeamRespository;
import cuit.game.dao.ContestRespository;
import cuit.game.entity.Team;


import jakarta.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Transactional
public class TeamService {
    @Autowired
    TeamRespository teamRespository;
    @Autowired
    ContestRespository contestRespository;

    /**
     * 查找所有
     * 
     * @return
     */
    public List<Team> findAll()  {
	return teamRespository.findAll();
    }

    /**
     * 分页查找
     * 
     * @return
     */
    public Page<Team> findAllByPage(int pageIndex, int pageSize) {
	//排序
	Sort sort = Sort.by(Sort.Direction.ASC, "id");
	//分页
	Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);
	//分页查询
	Page<Team> teamPage = teamRespository.findAll(pageable);
	return teamPage;
    }

   
    /**
     * 根据Name查找
     * 
     * @return
     */
    public List<Team> findByTeamnameLike(String teamname) {
	return teamRespository.findByTeamnameLike(teamname);
    }
    
    /**
     * 保存
     * 
     * @param article
     * @return
     */
    public Team save(Team team) {
	return teamRespository.save(team);
    }
    
    /**
     * 查找某条记录
     * 
     * @param id
     * @return
     */
    public Team findById(long id) {
	return teamRespository.findById(id).orElse(new Team());
    }

    /**
     * 删除某条记录
     * 
     * @param id
     */
    public void deleteById(long id) {
	teamRespository.deleteById(id);
    }

    public List<Team> findByContestId(long contestId) {
        return teamRespository.findByContestId(contestId);
    }
}
